//
//  AhriGalleryModel.swift
//  UUCartoon
//
//  Created by Galaxy on 2024/4/17.
//  Copyright © 2024 qykj. All rights reserved.
//

import UIKit
import Ji
import SwiftyJSON
class AhriGalleryModel: WebsiteBaseModel,WebsiteProtocol {
    override init() {
        super.init()
        websiteName = "Ahri-gallery"
//        webUrlStr = "https://ahri-gallery.com"
        webUrlStr = "https://ahri-gallery-xfjd-2024-04-25.top"
        websiteTitleArr = []
        websiteIdArr = []
        type = .ahri
    }
    //MARK: 排行列表
    func getCategorySiftList(condition: (category:String,reader:String,progress:String,area:String), pageNum: Int) -> [CartoonModel] {
        []
    }
    //MARK: 获取图片列表
    func getImageList(detailUrl: String) -> [CartoonImgModel] {
        let urlStr = "\(webUrlStr)/\(detailUrl)"
        let jiDoc = Ji.init(htmlURL: URL.init(string: urlStr)!)
        if jiDoc == nil {
            return []
        }else{
            var array:[CartoonImgModel] = []
            let contentUrlXpath = "//div[@class='btn-group action_btn']/a/@href"
            let contentUrlNodeArr = jiDoc?.xPath(contentUrlXpath)
            if contentUrlNodeArr!.count > 0 {
                var detailUrlStr = contentUrlNodeArr![0].content
                detailUrlStr = detailUrlStr?.replacingOccurrences(of: "./index.php", with: "\(self.webUrlStr)/index.php")
                let jiDoc = Ji.init(htmlURL: URL.init(string: detailUrlStr!)!)
                if jiDoc == nil {
                    return []
                }
                let jsXpath = "//div['main']/script/text()"
                let jsNodeArr = jiDoc?.xPath(jsXpath)
                if jsNodeArr!.count > 0 {
                    let jsStr = jsNodeArr![0].content
                    let jsArr3 = jsStr?.components(separatedBy: ";")
                    var imgUrlStr = jsArr3?.first
                    imgUrlStr = imgUrlStr?.replacingOccurrences(of: " ", with: "")
                    imgUrlStr = imgUrlStr?.replacingOccurrences(of: "\r\n", with: "")
                    imgUrlStr = imgUrlStr?.replacingOccurrences(of: "\"", with: "")
                    imgUrlStr = imgUrlStr?.replacingOccurrences(of: "varHTTP_IMAGE=", with: "")
                    let jsArr1 = jsStr?.components(separatedBy: "Image_List =")
                    let jsArr2 = jsArr1?.last?.components(separatedBy: ";")
                    let jsonStr = JSON.init(parseJSON: jsArr2!.first!)
                    for index in 0...jsonStr.count-1 {
                        var imgModel = CartoonImgModel.init()
                        let imgName = jsonStr[index]["sort"].stringValue
                        let FileExtension = jsonStr[index]["extension"].stringValue
                        imgModel.imgUrl = "https:\(imgUrlStr ?? "")\(imgName).\(FileExtension)"
                        imgModel.type = .ahri
                        array.append(imgModel)
                    }
                    return array
                }
                return array
            }else{
                return array
            }
        }
    }
    //MARK: 搜索结果
    func getSearchList(keyword: String, pageNum: Int) -> [CartoonModel] {
        []
    }
    //MARK: 搜索推荐
    func getSearchRecommendList() -> [CartoonModel] {
        []
    }
    //MARK: 获取漫画详情
    func getDetailData(urlStr: String) -> CartoonModel {
        CartoonModel.init()
    }
    //MARK: 已完成
    func getDoneList(pageNum: Int) -> [CartoonModel] {
        []
    }
    //MARK: 漫画类型
    func getCategoryList() -> [[CategoryModel]] {
        []
    }
    //MARK: 漫画排行
    func getRankList(pageNum: Int) -> [CartoonModel] {
        []
    }
    //MARK: 最新更新
    func getLatestList(pageNum:Int) -> [CartoonModel] {
        let urlStr = webUrlStr + "/index.php?route=comic/list&page=\(pageNum)"
        let  jiDoc = Ji.init(htmlURL: URL.init(string: urlStr)!)
        if jiDoc == nil {
            return []
        } else {
            var resultArr: [CartoonModel] = []
            //            标题
            let titleXPath = "//div[@class='image-info']/h5/a/@title"
            //            详情
            let urlXPath = "//div[@class='image-info']/h5/a/@href"
            // 作者
//            let authorXPath = ""
            // 时间
//            let timeXPath = ""
            // 图片
            let imgXPath = "//div[@class='image-inner']/a/img/@src"
            // 最新
//            let numXPath = ""
            let titleNodeArr = jiDoc?.xPath(titleXPath)
            let urlNodeArr = jiDoc?.xPath(urlXPath)
//            let authorNodeArr = jiDoc?.xPath(authorXPath)
//            let timeNodeArr = jiDoc?.xPath(timeXPath)
//            let numNodeArr = jiDoc?.xPath(numXPath)
            let imgNodeArr = jiDoc?.xPath(imgXPath)
            // 数据不为空
            if !(urlNodeArr?.isEmpty)! {
                for (index, urlNode) in urlNodeArr!.enumerated() {
                    let cartoonModel = CartoonModel.init()
                    cartoonModel.name = titleNodeArr![index].content!
                    cartoonModel.detailUrl = urlNode.content!
                    cartoonModel.author = ""
                    cartoonModel.category = ""
                    cartoonModel.time = ""
                    cartoonModel.type = .ahri
                    cartoonModel.num = ""
                    cartoonModel.imgUrl = "https:"+imgNodeArr![index].content!
                    let chapterModel = ChapterModel()
                    chapterModel.name = "默认"
                    chapterModel.detailUrl = cartoonModel.detailUrl
                    cartoonModel.chapterArr = [
                        (name:"默认",data:[chapterModel])
                    ]
                    resultArr.append(cartoonModel)
                }
            }
            return resultArr
        }
    }
}
